﻿using System;
using System.Collections.Generic;
using System.Text;

//session map
using SessionMap = System.Collections.Generic.Dictionary<int, int>;
//world list
using WorldList = System.Collections.Generic.List<int>;
//group map
using WorldGroupInfoMap = System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int>>;

/**
*@file       WorldGroup.cs
*@brief      group of world servers
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/02/15
*/

public class WorldGroup
{
    #region private variable
    //all session
    SessionMap m_SessionMap = new SessionMap();
    //list of information
    private WorldGroupInfoMap m_InfoMap = new WorldGroupInfoMap();
    //lock object
    Object m_lockObj = new Object();
    #endregion

    #region external call
    /// <summary>
    /// constructor
    /// </summary>
    /// <param name="file_name"></param>
    public bool LoadFromFile(string file_name)
    {
        try
        {
            using (System.IO.FileStream file = new System.IO.FileStream(file_name, System.IO.FileMode.Open))
            {
                global.WorldGroupInfo wgi = ProtoBuf.Serializer.Deserialize<global.WorldGroupInfo>(file);
                foreach (global.WorldGroupInfo.WorldGroup wg in wgi.Info)
                {
                    WorldList wl = new WorldList();
                    foreach (int id in wg.WorldId)
                    {
                        wl.Add(id);
                    }
                    m_InfoMap[wg.WorldGroupId] = wl;
                }

                return true;
            }
        }
        catch (System.Exception e)
        {
            System.Console.WriteLine("Can Not Read World Group Config File:{0} With Error:{1}", file_name, e.Message);
        }

        return false;
    }

    /// <summary>
    /// set group session identifier
    /// </summary>
    /// <param name="WorldGroup"></param>
    /// <param name="SessionId"></param>
    public void SetGroupSessionId(int WorldGroup, int SessionId)
    {
        lock (m_lockObj)
        {
            if (m_InfoMap.ContainsKey(WorldGroup) == true)
            {
                foreach (int id in m_InfoMap[WorldGroup])
                {
                    m_SessionMap[id] = SessionId;
                }
            }
        }
    }

    /// <summary>
    /// get wolrd session id
    /// </summary>
    /// <param name="WorldId"></param>
    /// <returns></returns>
    public int GetSessionId(int WorldId)
    {
        lock (m_lockObj)
        {
            if (m_SessionMap.ContainsKey(WorldId) == true)
                return m_SessionMap[WorldId];
        }

        return NetLib.INVALID_SESSION;
    }

    /// <summary>
    /// get the world list
    /// </summary>
    /// <param name="groupId"></param>
    /// <returns></returns>
    public WorldList GetWorldList(int groupId)
    {
        lock (m_lockObj)
        {
            WorldList worldList;
            if (m_InfoMap.TryGetValue(groupId, out worldList))
            {
                WorldList retList = new WorldList();
                retList.AddRange(worldList);
                return retList;
            }
        }

        return null;
    }

    /// <summary>
    /// find world group
    /// </summary>
    /// <param name="worldId"></param>
    /// <returns></returns>
    public int FindWorldGroup(int worldId)
    {
        lock (m_lockObj)
        {
            foreach (int group_id in m_InfoMap.Keys)
            {
                WorldList wl = m_InfoMap[group_id];

                foreach (int id in wl)
                {
                    if (id == worldId)
                        return group_id;
                }
            }
        }

        return GlobalDef.INVALID_WORLD_GROUP;
    }
    #endregion
}